package com.sanshi.project.assets.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sanshi.project.admin.api.entity.InspectRouteEntity;
import com.sanshi.project.assets.mapper.InspectRouteMapper;
import com.sanshi.project.assets.service.InspectRouteService;
import org.springframework.stereotype.Service;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sanshi.project.admin.api.entity.InspectRoutePointMapEntity;
import com.sanshi.project.assets.mapper.InspectRoutePointMapMapper;
import org.springframework.transaction.annotation.Transactional;
import lombok.RequiredArgsConstructor;
import java.util.Objects;
/**
 * 巡检路线
 *
 * @author nikon
 * @date 2024-01-01 17:17:53
 */
@Service
@RequiredArgsConstructor
public class InspectRouteServiceImpl extends ServiceImpl<InspectRouteMapper, InspectRouteEntity> implements InspectRouteService {
  private final InspectRoutePointMapMapper inspectRoutePointMapMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveDeep(InspectRouteEntity inspectRoute) {
        baseMapper.insert(inspectRoute);
        for (InspectRoutePointMapEntity  inspectRoutePointMap : inspectRoute.getInspectRoutePointMapList()) {
            inspectRoutePointMap.setRouteId(inspectRoute.getId());
            inspectRoutePointMapMapper.insert( inspectRoutePointMap);
        }

        return Boolean.TRUE;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateDeep(InspectRouteEntity inspectRoute) {
        baseMapper.updateById(inspectRoute);
        for (InspectRoutePointMapEntity  inspectRoutePointMap : inspectRoute.getInspectRoutePointMapList()) {
            if (Objects.isNull(inspectRoutePointMap.getId())) {
                inspectRoutePointMap.setRouteId(inspectRoute.getId());
                inspectRoutePointMapMapper.insert(inspectRoutePointMap);
            } else {
                inspectRoutePointMapMapper.updateById(inspectRoutePointMap);
            }
        }
        return Boolean.TRUE;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeDeep(Long[] ids) {
        baseMapper.deleteBatchIds(CollUtil.toList(ids));
        inspectRoutePointMapMapper.delete(Wrappers.<InspectRoutePointMapEntity>lambdaQuery().in(InspectRoutePointMapEntity::getRouteId, ids));
        return Boolean.TRUE;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeChild(Long[] ids) {
        inspectRoutePointMapMapper.deleteBatchIds(CollUtil.toList(ids));
        return Boolean.TRUE;
    }
}